﻿import axios from 'axios'
import qs from 'qs'

// axios 配置
axios.defaults.timeout = 10000;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
//axios.defaults.baseURL = '//localhost/cot/web/api2/';
//axios.defaults.baseURL = '//192.168.31.9/cot/web/api2/';
axios.defaults.baseURL = 'https://www.icottontrade.com/cot/web/api2/';

//POST传参序列化
axios.interceptors.request.use((config) => {
    if (config.method === 'post') {
        console.log(config)
        if(config.headers["Content-Type"]=='multipart/form-data'){
            // config.headers.post["Content-Type"]='multipart/form-data';
        }
        else
        config.data = qs.stringify(config.data);
    }
    return config;
}, (error) => {
    _.toast("错误的传参", 'fail');
    return Promise.reject(error);
});

//返回状态判断
axios.interceptors.response.use((res) => {
    // if (!res.data.success) {
    //     // _.toast(res.data.msg);
    //     return Promise.reject(res);
    // }
    return res;
}, (error) => {
    console.log(error);
    // _.toast("网络异常", 'fail');
    return Promise.reject(error);

    //this.axios.con
});

export function post(url, params) {
    return new Promise((resolve, reject) => {

        axios.post(url, params)
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error)
            })
    }).catch((error) => {
        console.log(error)
    })
}

export function get(url, params) {
    return new Promise((resolve, reject) => {

        axios.get(url, {params:params})
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error)
            })
    }).catch((error) => {
        console.log(error)
    })
}

export default {
    /**
     * 用户登录
     */
    login(params) {
        return get('login', params)
    },
    GetChatUser(params) {
        return get('GetChatUser', params)
    },
    getBatchList(params) {
        return get('getBatchList', params)
    },
    getBatchInfo(params) {
        return get('getBatchInfo', params)
    },
    getEntListByPYS(params) {
        return get('getEntListByPYS', params)
    },
    AddDemand(params) {
        return get('AddDemand', params)
    },
    getDemand(params) {
        return get('getDemand', params)
    },
    getDemandDetail(params) {
        return get('getDemandDetail', params)
    },
    delDemandByID(params) {
        return get('delDemandByID', params)
    },
    getBatchListByDID(params) {
        return get('getBatchListByDID', params)
    },
    addToCard(params) {
        return get('addToCard', params)
    },
    getUserInfo(params) {
        return get('GetUserInfo', params)
    },
    AddBatch(params) {
        return get('AddBatch', params)
    },
    getShopCartList(params) {
        return get('getShopCartList', params)
    },
    AddSupply(params) {
        return get('AddSupply', params)
    },
    getSupplyList(params) {
        return get('getSupplyList', params)
    },
    getSupplyDetail(params) {
        return get('getSupplyDetail', params)
    },
    AddBaoJia(params) {
        return get('AddBaoJia', params)
    },
    getBaoJiaList(params) {
        return get('getBaoJiaList', params)
    },
    addComplain(params) {
        return get('addComplain', params)
    },
    addPhoneLog(params) {
        return get('addPhoneLog', params)
    },
    getMyBatchList(params) {
        return get('getMyBatchList', params)
    },
    getMsgList(params) {
        return get('getMsgList', params)
    },
    delBatch(params) {
        return get('delBatch', params)
    },
    endSupply(params) {
        return get('endSupply', params)
    },
    delSupply(params) {
        return get('delSupply', params)
    },
    addUserMsg(params) {
        return get('addUserMsg', params)
    },
    getNewMsg(params) {
        return get('getNewMsg', params)
    },
    getNoticeCount(params) {
        return get('getNoticeCount', params)
    },
    getUserOpenID(params) {
        return get('getUserOpenID', params)
    },
    BindPhone(params) {
        return get('BindPhone', params)
    },
    getSupplyAll(params) {
        return get('getSupplyAll', params)
    },
    getUserInfoByID(params) {
        return get('GetUserInfoByID', params)
    },
    getwxConfig(params) {
        return get('getwxConfig', params)
    },
    SendSMS(params) {
        return get('SendSMS', params)
    },
    GenPic(params) {
        return get('genPic', params)
    },
    getEntInfo(params) {
        return get('getEntInfo', params)
    },
    getBatchInfoSingle(params) {
        return get('getBatchInfoSingle', params)
    },
    updateTZ(params) {
        return get('updateTZ', params)
    },
    downLoadWXPic(params) {
        return get('downLoadWXPic', params)
    },
    getStorage(params) {
        return get('getStorage', params)
    },
    deleteDemand(params) {
        return get('deleteDemand', params)
    },
    getBTNums(params) {
        return get('getBTNums', params)
    },
}